#!/usr/bin/env python3
# -*- coding: utf8 -*-

def get_leveled_config(key,  data, default_value = None):
    '''to get a key (in form of a/b/c) from 'data'

    Parameters
    -----------------------------------
        key: key name, separated by "/"
        data: dict to search
        default_value: when key is not found

    Returns
    -------------------------
    Data in dict or default
    '''
    for ele in key.split('/'):
        data = data.get(ele, None)
        if not data:
            return default_value

    return data


value_chinese_dict = {'零':0,
        '一':1,
        '二':2,
        '两':2,
        '三':3,
        '四':4,
        '五':5,
        '六':6,
        '七':7,
        '八':8,
        '九':9,
        '十':10,
    }

def _get_single_number(value):
    try:
        idx = value_chinese_dict.get(value, None)
    except:
        return None
    return idx

def get_number_by_chinese(value):
    value = value.replace('第', ' ').replace('章', ' ').replace('张', ' ')
    value = [ele for ele in value.split(' ') if len(ele) > 0 ][0]
    # 十是一个很特殊的单位，可以开头，也可以结尾，如十一，五十。而百、千，就不能说百五十一，千二十。

    idx = 0
    title_numbers = 0
    if value[0] == '十':
        title_numbers = 10
        idx = 1

    while idx < len(value):
        ch = value[idx]
        idx += 1
        if ch in ['零']:
            continue

        # if ch in ['万', '千', '百', '十']:
        #     continue
        # title_numbers = title_numbers*10

        if ch >= '0' and ch <= '9':
            tmp = 0
            while ch >= '0' and ch <= '9':
                tmp *= 10
                tmp += int(ch)
                if idx >= len(value):
                    break
                ch = value[idx]
                idx += 1

            title_numbers = tmp

        else:
            tmp = _get_single_number(ch)
            if tmp is None:
                print("%s is not found as a number."%(ch))
                return None

            if idx < len(value):
                if value[idx] == '十':
                    tmp *= 10
                    idx += 1
                elif value[idx] == '百':
                    tmp *= 100
                    idx += 1
                elif value[idx] == '千':
                    tmp *= 1000
                    idx += 1
                elif value[idx] == '万':
                    tmp *= 10000
                    idx += 1

            title_numbers += tmp

    return title_numbers


if __name__ == '__main__':

    assert get_number_by_chinese("第一章 惊蛰") == 1
    assert get_number_by_chinese("第十章 惊蛰") == 10
    assert get_number_by_chinese("第五十章 惊蛰") == 50
    assert get_number_by_chinese("第七十一章 有些喜欢") == 71
    assert get_number_by_chinese("第一百零四章 约战") == 104
    assert get_number_by_chinese("第一百四十九章 约战") == 149
    assert get_number_by_chinese("第五百章 惊蛰") == 500
    assert get_number_by_chinese("第五百零一章 惊蛰") == 501
    assert get_number_by_chinese("第五百九十七章 问拳之前便险峻") == 597

    assert get_number_by_chinese("第九零四章 排枪，又是排枪") == 904
    assert get_number_by_chinese("第九百七十五章") == 975

    assert get_number_by_chinese("第一千章 阵容") == 1000
    assert get_number_by_chinese("第一千零一章 天下十豪") == 1001


    assert get_number_by_chinese("第1章 天下十豪") == 1
    assert get_number_by_chinese("第1001章 天下十豪") == 1001


